package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.bo.ZjExperimentBindingBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryAsphaltCombustionFurnaceResBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryAsphaltCombustionFurnaceResVo;
import com.ruoyi.zhgd.domain.NLaboratoryAsphaltCombustionFurnaceRes;
import com.ruoyi.zhgd.mapper.NLaboratoryAsphaltCombustionFurnaceResMapper;
import com.ruoyi.zhgd.service.INLaboratoryAsphaltCombustionFurnaceResService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 燃烧法沥青含量测定仪（燃烧炉）_结果Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryAsphaltCombustionFurnaceResServiceImpl implements INLaboratoryAsphaltCombustionFurnaceResService {

    private final NLaboratoryAsphaltCombustionFurnaceResMapper baseMapper;

    /**
     * 查询燃烧法沥青含量测定仪（燃烧炉）_结果
     */
    @Override
    public NLaboratoryAsphaltCombustionFurnaceResVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询燃烧法沥青含量测定仪（燃烧炉）_结果列表
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltCombustionFurnaceResVo> queryPageList(NLaboratoryAsphaltCombustionFurnaceResBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryAsphaltCombustionFurnaceRes> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryAsphaltCombustionFurnaceResVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询燃烧法沥青含量测定仪（燃烧炉）_结果列表
     */
    @Override
    public List<NLaboratoryAsphaltCombustionFurnaceResVo> queryList(NLaboratoryAsphaltCombustionFurnaceResBo bo) {
        LambdaQueryWrapper<NLaboratoryAsphaltCombustionFurnaceRes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryAsphaltCombustionFurnaceRes> buildQueryWrapper(NLaboratoryAsphaltCombustionFurnaceResBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryAsphaltCombustionFurnaceRes> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryAsphaltCombustionFurnaceRes::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), NLaboratoryAsphaltCombustionFurnaceRes::getFlag, bo.getFlag());
        lqw.eq(bo.getOriginalWeight() != null, NLaboratoryAsphaltCombustionFurnaceRes::getOriginalWeight, bo.getOriginalWeight());
        lqw.eq(bo.getRealTimeWeight() != null, NLaboratoryAsphaltCombustionFurnaceRes::getRealTimeWeight, bo.getRealTimeWeight());
        lqw.eq(bo.getRealTimeTemp() != null, NLaboratoryAsphaltCombustionFurnaceRes::getRealTimeTemp, bo.getRealTimeTemp());
        lqw.eq(bo.getMin() != null, NLaboratoryAsphaltCombustionFurnaceRes::getMin, bo.getMin());
        lqw.eq(bo.getSecond() != null, NLaboratoryAsphaltCombustionFurnaceRes::getSecond, bo.getSecond());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalData()), NLaboratoryAsphaltCombustionFurnaceRes::getOriginalData, bo.getOriginalData());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleId()), NLaboratoryAsphaltCombustionFurnaceRes::getSampleId, bo.getSampleId());
        return lqw;
    }

    /**
     * 新增燃烧法沥青含量测定仪（燃烧炉）_结果
     */
    @Override
    public Boolean insertByBo(NLaboratoryAsphaltCombustionFurnaceResBo bo) {
        NLaboratoryAsphaltCombustionFurnaceRes add = BeanUtil.toBean(bo, NLaboratoryAsphaltCombustionFurnaceRes.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改燃烧法沥青含量测定仪（燃烧炉）_结果
     */
    @Override
    public Boolean updateByBo(NLaboratoryAsphaltCombustionFurnaceResBo bo) {
        NLaboratoryAsphaltCombustionFurnaceRes update = BeanUtil.toBean(bo, NLaboratoryAsphaltCombustionFurnaceRes.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryAsphaltCombustionFurnaceRes entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除燃烧法沥青含量测定仪（燃烧炉）_结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 燃烧法沥青含量测定仪（燃烧炉）_结果设备列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryAsphaltCombustionFurnaceResVo> page(NLaboratoryAsphaltCombustionFurnaceResBo bo, PageQuery pageQuery) {
        Page<NLaboratoryAsphaltCombustionFurnaceResVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询已关联数据
     *
     * @param build
     * @param bo
     * @return
     */
    @Override
    public Page<Map<String, Object>> getRelPage(Page<Object> build, ZjExperimentBindingBo bo) {
        return baseMapper.getRelPage(build, bo);
    }

    /**
     * 是否关联是否合格状态更新
     *
     * @param laboratoryId
     * @param eligible
     * @param relevance
     */
    @Override
    public void updateStatus(String laboratoryId, Integer eligible, Integer relevance) {
        baseMapper.updateStatus(laboratoryId, eligible, relevance);
    }
}
